/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view;

import canteen.sys.model.generic.HibernateDAOFactory;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TNT
 */
public class SubDepartmentEntry extends javax.swing.JInternalFrame  {

    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultTableModel modelTable;

    /**
     * Creates new form SubDepartmentEntry
     */
    public SubDepartmentEntry() {
        initComponents();
//        modelTable = (DefaultTableModel) tableSubDepartment.getModel();
//        getListDepartment();
//        List<SubDepartmentMaster> listSubDepartment = hibernateDAOFactory.getSubDepartmentMasterDAO().findAll();
//        showList(listSubDepartment);
//        listSelectionListener();
//        listenerTableModel();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btsave = new javax.swing.JButton();
        btdelete = new javax.swing.JButton();
        btcancel = new javax.swing.JButton();
        txtSubDepID = new javax.swing.JTextField();
        txtSubDepName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        rbStatus = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        cbDepartmentName = new javax.swing.JComboBox();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableSubDepartment = new javax.swing.JTable();

        setClosable(true);
        setTitle("Sub Department Entry");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Box"));
        jLayeredPane1.setToolTipText("");

        jLabel1.setText("Sub Department ID");
        jLabel1.setBounds(10, 30, 130, 14);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Department Name");
        jLabel2.setBounds(10, 60, 130, 14);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btsave.setText("Save");
        btsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btsaveActionPerformed(evt);
            }
        });
        btsave.setBounds(10, 170, 80, 23);
        jLayeredPane1.add(btsave, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btdelete.setText("Delete");
        btdelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdeleteActionPerformed(evt);
            }
        });
        btdelete.setBounds(110, 170, 80, 23);
        jLayeredPane1.add(btdelete, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btcancel.setText("Cancel");
        btcancel.setEnabled(false);
        btcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcancelActionPerformed(evt);
            }
        });
        btcancel.setBounds(210, 170, 80, 23);
        jLayeredPane1.add(btcancel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtSubDepID.setEnabled(false);
        txtSubDepID.setBounds(150, 30, 190, 20);
        jLayeredPane1.add(txtSubDepID, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtSubDepName.setBounds(150, 90, 190, 20);
        jLayeredPane1.add(txtSubDepName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Status");
        jLabel3.setBounds(10, 120, 90, 14);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rbStatus.setSelected(true);
        rbStatus.setBounds(150, 120, 21, 21);
        jLayeredPane1.add(rbStatus, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Sub Department Name");
        jLabel4.setBounds(10, 90, 130, 14);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbDepartmentName.setBounds(150, 60, 190, 20);
        jLayeredPane1.add(cbDepartmentName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Click on Record to Delete or Edit"));

        tableSubDepartment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID", "Department Name", "Sub Department Name", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableSubDepartment.setToolTipText("");
        jScrollPane1.setViewportView(tableSubDepartment);
        tableSubDepartment.getColumnModel().getColumn(0).setMinWidth(50);
        tableSubDepartment.getColumnModel().getColumn(0).setPreferredWidth(50);
        tableSubDepartment.getColumnModel().getColumn(0).setMaxWidth(50);
        tableSubDepartment.getColumnModel().getColumn(3).setMinWidth(100);
        tableSubDepartment.getColumnModel().getColumn(3).setPreferredWidth(100);
        tableSubDepartment.getColumnModel().getColumn(3).setMaxWidth(100);

        jScrollPane1.setBounds(10, 20, 470, 370);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 359, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 217, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 198, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btsaveActionPerformed
        // TODO add your handling code here:
//        try {
//            SubDepartmentMaster subDepartment = new SubDepartmentMaster();
//            if (btsave.getText().equals("Save")) {
//                subDepartment.setSubDepartmentName(txtSubDepName.getText());
//                ComboboxRecInfo<DepartmentMaster> combobox = (ComboboxRecInfo<DepartmentMaster>) cbDepartmentName.getSelectedItem();
//                subDepartment.setDepartment(combobox.getKeyData());
//                subDepartment.setStatus(rbStatus.isSelected());
//                validSaveOrUpdateSubDepartment(subDepartment);
//            } else {
//                subDepartment.setSubDeptId(Integer.parseInt(txtSubDepID.getText()));
//                subDepartment.setSubDepartmentName(txtSubDepName.getText());
//                ComboboxRecInfo<DepartmentMaster> combobox = (ComboboxRecInfo<DepartmentMaster>) cbDepartmentName.getSelectedItem();
//                subDepartment.setDepartment(combobox.getKeyData());
//                subDepartment.setStatus(rbStatus.isSelected());
//                validSaveOrUpdateSubDepartment(subDepartment);
//            }
//            this.txtSubDepName.requestFocus();
//        } catch (HibernateException ex) {
//            JOptionPane.showMessageDialog(this, "Add new Sub DepartmentEntry Fail!\n Error: " + ex.getMessage(),
//                    "Error", JOptionPane.ERROR_MESSAGE);
//        }
    }//GEN-LAST:event_btsaveActionPerformed

    private void btdeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btdeleteActionPerformed
        // TODO add your handling code here:
//        int indexRow = tableSubDepartment.getSelectedRow();
//        if (indexRow != -1) {
//            try {
//                if (modelTable.getRowCount() > 1) {
//                    hibernateDAOFactory.getSubDepartmentMasterDAO().makeTransient(getObject(indexRow));
//                    deleteRow(indexRow);
//                }
//            } catch (HibernateException ex) {
//                JOptionPane.showMessageDialog(this, "Can't delete Sub Department\nError: " + ex.getMessage());
//            }
//        } else {
//            JOptionPane.showMessageDialog(this, "No row selected!", "Warning", JOptionPane.WARNING_MESSAGE);
//        }
    }//GEN-LAST:event_btdeleteActionPerformed

    private void btcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcancelActionPerformed
        // TODO add your handling code here:
        //        DepartmentMaster departmentMaster = new DepartmentMaster();
        //        departmentMaster.setDepartmentName("TNTuyen");
        //        departmentMaster.setStatus(false);
        //        updateRow(departmentMaster, 2);
//        this.dispose();
        btsave.setText("Save");
        btcancel.setEnabled(false);
    }//GEN-LAST:event_btcancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btcancel;
    private javax.swing.JButton btdelete;
    private javax.swing.JButton btsave;
    private javax.swing.JComboBox cbDepartmentName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbStatus;
    private javax.swing.JTable tableSubDepartment;
    private javax.swing.JTextField txtSubDepID;
    private javax.swing.JTextField txtSubDepName;
    // End of variables declaration//GEN-END:variables
//
//    @Override
//    public void deleteRow(int index) {
//        if (modelTable.getRowCount() > 0) {
//            if (index > 0) {
//                tableSubDepartment.setRowSelectionInterval(index - 1, index - 1);
//                modelTable.removeRow(index);
//            } else {
//                tableSubDepartment.setRowSelectionInterval(index + 1, index + 1);
//                modelTable.removeRow(0);
//            }
//        } else {
//            JOptionPane.showMessageDialog(this, "There is no row to delete!", "Warning!", JOptionPane.WARNING_MESSAGE);
//        }
//    }
//
//    @Override
//    public void updateRow(SubDepartmentMaster row, int index) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    @Override
//    public void addRow(SubDepartmentMaster row, boolean posion) {
//        String status = "disable";
//        if (row.isStatus()) {
//            status = "enable";
//        }
//        if (posion) {
//            modelTable.insertRow(0, new Object[]{row.getSubDeptId() + "",
//                        row.getDepartment().getDepartmentName(),
//                        row.getSubDepartmentName() + "", status});
//        } else {
//            modelTable.insertRow(tableSubDepartment.getRowCount(), new Object[]{row.getSubDeptId() + "",
//                        row.getDepartment().getDepartmentName(),
//                        row.getSubDepartmentName() + "", status});
//        }
//    }
//
//    @Override
//    public final void showList(List<SubDepartmentMaster> list) {
//        String status;
//        for (int i = 0; i < 4; i++) {
//            modelTable.removeRow(0);
//        }
//        for (SubDepartmentMaster item : list) {
//            if (item.isStatus()) {
//                status = "enable";
//            } else {
//                status = "disable";
//            }
//            modelTable.insertRow(0, new Object[]{item.getSubDeptId() + "",
//                        item.getDepartment().getDepartmentName(),
//                        item.getSubDepartmentName() + "", status});
//        }
//    }
//
//    @Override
//    public void showDetail(SubDepartmentMaster object) {
//        txtSubDepID.setText(object.getSubDeptId() + "");
//        txtSubDepName.setText(object.getSubDepartmentName());
//        cbDepartmentName.setSelectedItem(object.getDepartment().getDepartmentName());
//        rbStatus.setSelected(object.isStatus());
//    }
//
//    @Override
//    public SubDepartmentMaster getObject(int index) {
//        SubDepartmentMaster subDepartmentMaster = new SubDepartmentMaster();
//        boolean status = false;
//        subDepartmentMaster.setSubDeptId(Integer.parseInt(modelTable.getValueAt(index, 0).toString()));
//        int indexCb = ComboboxRecInfo.findCbKeyDataIndex(cbDepartmentName, modelTable.getValueAt(index, 1).toString(), 0);
//        ComboboxRecInfo<DepartmentMaster> cbRecInfo = (ComboboxRecInfo) cbDepartmentName.getItemAt(indexCb);
//        DepartmentMaster department = cbRecInfo.getKeyData();
//        subDepartmentMaster.setDepartment(department);
//        subDepartmentMaster.setSubDepartmentName(modelTable.getValueAt(index, 2).toString());
//        if (modelTable.getValueAt(index, 3).equals("enable")) {
//            status = true;
//        }
//        subDepartmentMaster.setStatus(status);
//        return subDepartmentMaster;
//    }
//
//    @Override
//    public boolean validObject(SubDepartmentMaster object) {
//        if (object.getSubDepartmentName().equals("") || object.getSubDepartmentName() == null) {
//            JOptionPane.showMessageDialog(this, "Sub Department Name must input!");
//            return false;
//        }
//        return true;
//    }
//
//    public final void getListDepartment() {
//        List<DepartmentMaster> listDepartment = hibernateDAOFactory.getDepartmentMasterDAO().findAllByStatus(true);
//        if (listDepartment.size() > 0) {
//
//            for (DepartmentMaster departmentMaster : listDepartment) {
//                ComboboxRecInfo<DepartmentMaster> combobox = new ComboboxRecInfo<>();
//                combobox.setName(departmentMaster.getDepartmentName());
//                combobox.setKeyData(departmentMaster);
//                cbDepartmentName.addItem(combobox);
//            }
//        }
//    }
//
//    public final void listenerTableModel() {
//        modelTable.addTableModelListener(new TableModelListener() {
//            @Override
//            public void tableChanged(TableModelEvent e) {
//                if (e.getType() == TableModelEvent.DELETE) {
//                    JOptionPane.showMessageDialog(null, "Delete Success!");
//                }
//                if (e.getType() == TableModelEvent.INSERT) {
//                    JOptionPane.showMessageDialog(null, "Add new Sub Department Success!");
//                }
//            }
//        });
//    }
//
//    public final void listSelectionListener() {
//        tableSubDepartment.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
//            @Override
//            public void valueChanged(ListSelectionEvent e) {
//                int rowSelected = tableSubDepartment.getSelectedRow();
//                showDetail(getObject(rowSelected));
//            }
//        });
//    }
//
//    @Override
//    public void setObject(SubDepartmentMaster object) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//
//    private void validSaveOrUpdateSubDepartment(SubDepartmentMaster subDepartment) {
//        if (validObject(subDepartment)) {
//            SubDepartmentMaster departmentResult = hibernateDAOFactory.getSubDepartmentMasterDAO().makePersistent(subDepartment);
//            showDetail(departmentResult);
//            if (btsave.getText().equals("Save")) {
//                addRow(subDepartment, true);
//            } else {
//                updateRow(subDepartment, tableSubDepartment.getSelectedRow());
//            }
//        }
//    }
}
